package org.snail.chat.service.chatchain.chain;

import lombok.extern.log4j.Log4j;
import org.snail.chat.service.chatchain.dao.ChatCreateOrUpdateDAO;
import org.snail.constant.ErrCodeConstant;
import org.snail.dept.bean.TreeNode;
import org.snail.exception.CommonException;
import org.springframework.util.StringUtils;

import java.util.Map;

@Log4j
public class OwnerCreateOrUpdateHandler extends ChatCreateOrUpdateHandler {

    @Override
    public void doCreateOrUpdate(TreeNode treeNode, Map<String, ChatCreateOrUpdateDAO> chatCreateOrUpdateDAOMap) throws Exception {
        if (null == treeNode || StringUtils.isEmpty(treeNode.getId())) {
            log.error("treeNode中缺乏重要参数！treeNode :" + treeNode);
            throw new CommonException(ErrCodeConstant.ILLEGAL_PARAM, "treeNode中缺乏重要参数！treeNode :" + treeNode);
        }

        //获取当前树的ChatCreateOrUpdateDAO
        ChatCreateOrUpdateDAO chatCreateOrUpdateDAO = getChatCreateOrUpdateDAO(treeNode, chatCreateOrUpdateDAOMap);

        //设置所有者
        chatCreateOrUpdateDAO.setOwnerId(this.getHandlerRegular().getOwner());

        //重新放回到Map中
        chatCreateOrUpdateDAOMap.put(treeNode.getId(), chatCreateOrUpdateDAO);
    }

}
